#include <bits/stdc++.h>
using namespace std;

inline int read() {
	int x = 0, f = 0; char ch = getchar();
	while (ch < '0' or ch > '9') f |= (ch == '-'), ch = getchar();
	while (ch >= '0' and ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

int __stk[128], __top;
inline void write(int x) {
    if(x < 0) putchar('-'), x = -x;
	do { __stk[++__top] = x % 10, x /= 10; } while (x);
	while (__top) putchar(__stk[__top--] + '0');
}

const int mod = 1e9 + 7;

void Min(int &x, int y) { y < x and (x = y); }
void Max(int &x, int y) { y > x and (x = y); }

void inc(int &x, int y) { (x += y) >= mod and (x -= mod); }
void mul(int &x, int y) { x = 1ll * x * y % mod; }

int q_pow(int x, int k) { int res = 1; for (; k; k >>= 1, mul(x, x)) if (k & 1) mul(res, x); return res; }

bool stmer;

const int N = 2010;

int n;
int a[N], b[N], fac[N], inv[N];

int C(int x, int y) { return (y > x or y < 0) ? 0 : 1ll * fac[x] * inv[y] % mod * inv[x - y] % mod; }

void init() {
    fac[0] = 1;
    for (int i = 1; i < N; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
    inv[N - 1] = q_pow(fac[N - 1], mod - 2);
    for (int i = N - 1; i; i--) inv[i - 1] = 1ll * inv[i] * i % mod;
}

namespace sub1 {
	vector<int> p;
	map<vector<int>, int> mp;

	void check() { mp[p] = 1; }

	void dfs(int x, int y) {
		if (x + 1 == n and y + 1 == n) return check();
		if (x + 1 < n) p[x + y] = b[y], dfs(x + 1, y);
		if (y + 1 < n) p[x + y] = a[x], dfs(x, y + 1);
	}

	void main() {
		p.resize(2 * n - 2), dfs(0, 0);
		write(mp.size()), putchar('\n');
	}
}

namespace sub2 {
	bool check() {
		for (int i = 0; i < n; i++) 
			if (a[i] != i + 1 or b[i] != i + 1) return 0;
		return 1;
	}
	
	void main() {
		n--;
		int ans = (C(2 * n, n) - C(2 * n, n - 1) + mod) % mod;
		write(ans), putchar('\n');
	}
}

namespace sub3 {
	bool check() {
		for (int i = 0; i < n; i++) 
			if (a[i] != i + 1 or b[i] != n - i) return 0;
		return 1;
	}

	void main() {
		n--;
		int ans = (C(2 * n, n) - C(2 * n, n - 1) + mod) % mod;
		mul(ans, n + 1), write(ans), putchar('\n');
	}
}

bool edmer;
signed main() {
	freopen("zha.in", "r", stdin);
	freopen("zha.out", "w", stdout);
	cerr << "[Memory] " << (&stmer - &edmer) / 1024 / 1024 << " MB\n";
	
	init();

    n = read();
    for (int i = 0; i < n; i++) a[i] = read();
    for (int i = 0; i < n; i++) b[i] = read();

	if (n <= 10) sub1 :: main();
	else if (sub2 :: check()) sub2 :: main();
	else if (sub3 :: check()) sub3 :: main();
	else sub1 :: main();

    cerr << "[Runtime] " << (double) clock() / CLOCKS_PER_SEC << " seconds\n";
	return 0;
} 